import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { SystemMenuStateType } from './typings'
import {
  getSystemMenuList,
  getSystemMenuInfo,
  optionSystemMenuInfo,
  deleteSystemMenuInfo,
  getMenuTreeByParentId
} from '~/api/system/systemMenu'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

// 按钮功能下拉列表
const buttonFeaturesSelectList = [
  {
    key: 1,
    value: '工具栏'
  },
  {
    key: 2,
    value: '操作栏'
  },
  {
    key: 3,
    value: '工具操作栏'
  }
]
// 新窗口下拉列表
const newWindowSelectList = [
  {
    key: 0,
    value: '否'
  },
  {
    key: 1,
    value: '是'
  }
]
// 系统菜单类型
const menuSystemTypeSelectList = [
  {
    key: 0,
    value: 'PC'
  },
  {
    key: 1,
    value: 'app'
  }
]

const systemMenu: Module<SystemMenuStateType, SystemMenuStateType> = {
  namespaced: true,
  state: {
    menuTreeSelectList: [],
    menuTypeSelectList: [],
    menuTypeEnum: {},
    buttonFeaturesSelectList,
    buttonFeaturesEnum: filterValueEnum<SystemMenuStateType['buttonFeaturesEnum']>(
      buttonFeaturesSelectList,
      'key',
      'value'
    ) as SystemMenuStateType['buttonFeaturesEnum'],
    newWindowSelectList,
    newWindowEnum: filterValueEnum<SystemMenuStateType['newWindowEnum']>(
      newWindowSelectList,
      'key',
      'value'
    ) as SystemMenuStateType['newWindowEnum'],
    menuSystemTypeSelectList,
    menuSystemTypeEnum: filterValueEnum<SystemMenuStateType['menuSystemTypeEnum']>(
      menuSystemTypeSelectList,
      'key',
      'value'
    ) as SystemMenuStateType['menuSystemTypeEnum']
  },
  getters: {
    menuTreeSelectList: (state) => state.menuTreeSelectList,
    menuTypeSelectList: (state) => state.menuTypeSelectList,
    menuTypeEnum: (state) => state.menuTypeEnum,
    buttonFeaturesSelectList: (state) => state.buttonFeaturesSelectList,
    buttonFeaturesEnum: (state) => state.buttonFeaturesEnum,
    newWindowSelectList: (state) => state.newWindowSelectList,
    newWindowEnum: (state) => state.newWindowEnum,
    menuSystemTypeSelectList: (state) => state.menuSystemTypeSelectList,
    menuSystemTypeEnum: (state) => state.menuSystemTypeEnum
  },
  mutations: {
    SET_MENU_TREE_SELECT_LIST: (state, payload) => {
      state.menuTreeSelectList = payload
    },
    SET_MENU_TYPE_SELECT_LIST: (state, payload) => {
      state.menuTypeSelectList = payload
      state.menuTypeEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    }
  },
  actions: {
    // 获取列表信息
    async GetSystemMenuList<S, T> (_: ActionContext<S, T>, params: T) {
      return await getSystemMenuList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取菜单详情
    async GetSystemMenuInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return await getSystemMenuInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // (添加|修改)菜单管理项
    async OptionSystemMenuInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return await optionSystemMenuInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 删除菜单管理项
    async DeleteSystemMenuInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return await deleteSystemMenuInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取上级菜单树形列表
    async GetMenuTreeByParentId<S, T> ({ commit }: ActionContext<S, T>) {
      return await getMenuTreeByParentId({ code: 'menu_category' }).then((res: AxiosResponse) => {
        commit('SET_MENU_TREE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 获取菜单类型数据字典
    async GetMenuTypeSelectList<S, T> ({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'menu_category' }).then((res: AxiosResponse) => {
        commit('SET_MENU_TYPE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    }
  }
}

export default systemMenu
